import time
from urllib.parse import urlencode, urlparse
# from playwright.sync_api import sync_playwright


# 启动 Edge 浏览器
def read_main(url):
    url = remake_url(url)
    return url
    # with sync_playwright() as playwright:
    #     browser = playwright.chromium.launch(channel='msedge', headless=False)
    #     context = browser.new_context(
    #         bypass_csp=True,  # 禁用CSP检测
    #         ignore_https_errors=True,  # 忽略HTTPS错误
    #         java_script_enabled=True,  # 启用JavaScript
    #         is_mobile=False)  # 模拟非移动设备
    #     page = context.new_page()
    #     page.goto(url)
    #     # 等待 iframe 加载完成
    #     page.wait_for_selector('iframe', state='attached')
    #     # 获取 iframe 的内容
    #     frames = page.frames
    #     frame = frames[1]
    #     frame.wait_for_selector('#__reading__mode__mainbody__id', state='visible')
    #     html = frame.content()
    #     # html = page.content()
    #     context.close()
    #     browser.close()
    #     return html


def remake_url(url):
    # 获取 URL 的协议、域名和路径
    parsed_url = urlparse(url)
    protocol = parsed_url.scheme
    domain = parsed_url.netloc
    # 构造 query 参数
    query_params = {'url': url}
    query_string = urlencode(query_params)
    return f'read://{protocol}_{domain}/?{query_string}'


if __name__ == '__main__':
    st = time.time()
    print(read_main("https://www.maigoo.com/goomai/191273.html"))
    print(time.time() - st)
